﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls.Containers;
using UIT.Controls;
using UIT.Controls.Input;
using UIT.Controls.Data;

namespace UIT.Web
{
    class EventRaiserVisitor
    {
        private System.Web.HttpContext context;

        public EventRaiserVisitor(System.Web.HttpContext context)
        {
            // TODO: Complete member initialization
            this.context = context;
            eventTarget = context.Request.Form["__EVENTTARGET"];
        }

        #region IVisitor<Button> Members

        public void Visit(Button visitable)
        {
            if (context.Request.Form["__EVENTTARGET"] == visitable.Id || context.Request.Form[visitable.Id] != null)
                visitable.RaiseClick();
        }

        #endregion

        #region IVisitor<Page> Members

        public void Visit(Page visitable)
        {
            Visit((IControlContainer)visitable);
        }

        private void Visit(IControlContainer container)
        {
            foreach (var control in container.Children)
                this.Visit(control);
        }

        #endregion

        #region IVisitor<TextBox> Members

        public void Visit(TextBox visitable)
        {
        }

        public void Visit(DataGrid visitable)
        {
            if (context.Request.Form["__EVENTTARGET"] == visitable.Id)
            {
                var settingsQuery = context.Request.Form[visitable.Id];
                foreach (var setting in settingsQuery.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    int indexOfEqual = setting.IndexOf('=');
                    if (indexOfEqual > 0)
                    {
                        switch (setting.Substring(0, indexOfEqual))
                        {
                            case "sort":
                                visitable.SortExpression = setting.Substring(indexOfEqual + 1);
                                break;
                            case "page":
                                visitable.PageIndex = int.Parse(setting.Substring(indexOfEqual + 1));
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }

        #endregion

        #region IVisitor<IControl> Members

        string eventTarget;

        public void Visit(IControl visitable)
        {
            if (visitable.IsContainer && visitable.Id != eventTarget)
            {
                Visit((IControlContainer)visitable);
                return;
            }
            switch (visitable.Type)
            {
                case UIT.Controls.ControlType.Button:
                    Visit((Button)visitable);
                    break;
                case UIT.Controls.ControlType.TextBox:
                    Visit((TextBox)visitable);
                    break;
                case UIT.Controls.ControlType.List:
                    Visit((DataGrid)visitable);
                    break;
                default:
                    break;
            }
        }

        #endregion
    }
}
